package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.ExperimentSampleSmoke;
import com.siwei.mes.entity.experiment.concrete.UpdateKsdjSmoke;
import com.siwei.mes.service.experiment.ExperimentSampleSmokeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账抽样相关
 *
 * @author linzi
 * @since 2023-12-28 20:47:31
 */
@Api(value = "试验台账抽样")
@RestController
@RequestMapping("experimentSampleSmoke")
public class ExperimentSampleSmokeController {
    @Resource
    private ExperimentSampleSmokeService experimentSampleSmokeService;

    @ApiOperation(value = "新增试验台账抽样")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) ExperimentSampleSmoke entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentSampleSmokeService.create(entity)));
    }

    @ApiOperation(value = "修改试验台账抽样")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentSampleSmoke entity) {
        experimentSampleSmokeService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "修改试验台账抽样")
    @PostMapping("/updateList")
    public Json updateList(@RequestBody ExperimentSampleSmoke entity) {
        List<ExperimentSampleSmoke> smokeList = entity.getSmokeList();
        assertTrue(smokeList.isEmpty(), "请提供试验台账抽样记录信息");
        for (ExperimentSampleSmoke sampleSmoke : smokeList) {
            experimentSampleSmokeService.update(sampleSmoke);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除试验台账抽样")
    @PostMapping("/delete")
    public Json delete(@RequestBody ExperimentSampleSmoke entity) {
        experimentSampleSmokeService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取试验台账抽样")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ExperimentSampleSmoke, ExperimentSampleSmoke> query) {
        return Json.succ().data(experimentSampleSmokeService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取试验台账抽样")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentSampleSmokeService.loadById(id));
    }

    @ApiOperation(value = "获取所有试验台账抽样")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentSampleSmoke entity) {
        return Json.succ().data(ApiResult.addList(experimentSampleSmokeService.getAll(entity)));
    }

    @ApiOperation(value = "获取所有抗渗等级")
    @GetMapping("getKsDetail")
    public Json getKsDetail(@RequestParam("experimentId") Long experimentId) {
        return Json.succ().data(experimentSampleSmokeService.getKsDetail(experimentId));
    }

    @ApiOperation(value = "修改所有抗渗等级json")
    @PostMapping("updateKsDetail")
    public Json updateKsDetail(@RequestBody @Validated UpdateKsdjSmoke updateKsdjSmoke) {
        experimentSampleSmokeService.updateKsDetail(updateKsdjSmoke);
        return Json.succ().data(new ApiResult());
    }

}
